package name;

/**
 * Places a random character of a specified set between phonetics with a specified probability.
 * 
 * @author Catastrophe
 */
public final class InterruptedNameBuilderMethod implements NameBuilderMethod
{
	/**
	 * The probability that a character will be inserted between two phonetics.  Must be between 0 and 1.
	 */
	private double probability;
	
	/**
	 * The set of characters that may be inserted between two phonetics.
	 */
	private char [] characters = {'-', '\''};
	
	/**
	 * Creates a new InterruptedNameBuilderMethod.
	 * 
	 * @param probability the probability that a random character will be inserted between two phonetics (0 to 1)
	 * @param characters the set of characters that may appear between phonetics
	 * @throws IllegalArgumentException if the specified probability is not between 0 and 1
	 */
	public InterruptedNameBuilderMethod(double probability, char [] characters) throws IllegalArgumentException
	{
		this(probability);
		this.characters = characters;
	}
	
	/**
	 * Creates a new InterruptedNameBuilderMethod.
	 * 
	 * @param probability the probability that a random character will be inserted between two phonetics (0 to 1)
	 * @throws IllegalArgumentException if the specified probability is not between 0 and 1
	 */
	public InterruptedNameBuilderMethod(double probability) throws IllegalArgumentException
	{
		if(probability < 0 || probability > 1)
			throw new IllegalArgumentException();
		this.probability = probability;
	}
	
	/**
	 * Creates a new InterruptedNameBuilderMethod with a 10% chance of interruption.
	 */
	public InterruptedNameBuilderMethod()
	{
		this(0.1);
	}
	
	public void buildName(String[] phoneticList)
	{
		for(int i = 0; i < phoneticList.length; ++i)
			if(Math.random() < probability && i != phoneticList.length - 1)
				phoneticList[i] += characters[(int)(Math.random() * characters.length)];
	}
}